package com.wodsy.phoenix.datadao.shopc.dao.impl;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.shopc.dao.ShopFavoriteDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.shopc.model.ShopFavorite;
import com.wodsy.phoenix.dataobject.shopc.model.ShopFavoriteId;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository("shopFavoriteDao")
public class ShopFavoriteDaoImpl extends BaseHibernateDao<ShopFavorite, ShopFavoriteId> implements ShopFavoriteDao {
	@Autowired
	@Qualifier(value="shopc")
	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Class<ShopFavorite> getModelClass() {
		return ShopFavorite.class;
	}

	@Override
	public void addShopFavorite(Integer userId, Integer shopId) {
		ShopFavoriteId id = new ShopFavoriteId(shopId, userId);
		ShopFavorite favorite = new ShopFavorite(id, new Date());
		save(favorite);
	}

	@Override
	public void delShopFavorite(Integer userId, Integer shopId) {
		ShopFavoriteId id = new ShopFavoriteId(shopId, userId);
		ShopFavorite favorite = new ShopFavorite(id, new Date());
		delete(favorite);
	}

	@Override
	public Pager<ShopFavorite> findShopFavoritesByUserId(Pager<ShopFavorite> pager,Integer userId) {
		Criteria criteria = getSession().createCriteria(getModelClass());
		criteria.add(Restrictions.eq("id.userId", userId));
		criteria.addOrder(Order.desc("fvtTime"));
		toPager(pager, criteria);
		return pager;
	}

	@Override
	public ShopFavorite getShopFavorite(Integer userId, Integer shopId) {
		ShopFavorite shopFavorite = get(new ShopFavoriteId(shopId, userId));
		return shopFavorite;
	}

}
